package com.cw.controller;

import com.cw.beans.Role;
import com.cw.beans.User;
import com.cw.mapper.RoleMapper;
import com.cw.mapper.UserMapper;
import com.cw.service.UserService;
import com.cw.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    UserServiceImpl userServiceImpl;
    @Autowired
    private UserMapper userMapper;

    @ResponseBody
    @RequestMapping("/getUserList")
    public Object getUserList(){
        Map map =  new HashMap<>();
        ArrayList<User> userList = userServiceImpl.getUserList();
        List<Role> role = roleMapper.getRole();
        map.put("userList",userList);
        map.put("role",role);
        return map ;

    }
    // 回显
    @ResponseBody
    @RequestMapping("/upUser")
    public Object getUserList2(String id ){
        Map map =  new HashMap<>();
         if(id==""||id==null){
             map.put("msg","id不能为空");
         }else {
             int id1 = Integer.parseInt(id);
             User  user  = userServiceImpl.getUserById(id1);
           map.put("user",user);
         }
        return map ;
    }
    // 修改的方法
    @ResponseBody
    @RequestMapping("/upUser2")
    public Object getUserList3(String id,String name,String username,String password,String phone,String email,String  rid){
        Map map =  new HashMap<>();
       User user = new User();
       user.setId(Integer.parseInt(id));
       user.setName(name);
       user.setUsername(username);
       user.setPassword(password);
       user.setPhone(phone);
       user.setEmail(email);
       user.setRid(Integer.parseInt(rid));
        userServiceImpl.updateUser(user,map);
        return map ;
    }

    // 添加
    @ResponseBody
    @RequestMapping("/addUser")
    public Object getUserList4(String name,String username,String password,String phone,String email,String  rid){
        Map map =  new HashMap<>();
        User user = new User();
        user.setName(name);
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(phone);
        user.setEmail(email);
        user.setRid(Integer.parseInt(rid));
         userServiceImpl.addUser(user);
        ArrayList<User> userList = userServiceImpl.getUserList();
        map.put("userList",userList);
        return map ;

    }

    @ResponseBody
    @RequestMapping("/del")
    public  Object getUserList5(String id){
        Map map =  new HashMap<>();
        int id1 = Integer.parseInt(id);
        userMapper.del(id1);
        ArrayList<User> userList = userMapper.getUserList();
        map.put("list",userList);
        return map;
    }
}
